﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace POS_NEW
{
    public partial class frmDonVi : Form
    {
        bool _flag = false; 
        public frmDonVi()
        {
            InitializeComponent();
        }
        private void frmDonVi_Load(object sender, EventArgs e)
        {
            Clear();
        }
        private void Clear()
        {
            _flag = false;
            txtDonVi.Text = "";
            txtMota.Text = "";
            txtDonVi.Focus();
            txtDonVi.SelectAll();
            btnAdd.Enabled = true;
            btnSave.Enabled = true;
            btnExit.Enabled = true;
            myProgress.gridviewNoneParam("spDonVi_SelectALL", grDonVi);
        }
        #region --------Keydown và Button Click-----------------
        private void txtButton_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnButton_Click("1", e);
            }
            if (grDonVi.Focused == true && e.KeyCode == Keys.Delete)
            {
                using (ConfirmScreen frmcf = new ConfirmScreen("Xóa đơn vị " + grDonVi.ActiveRow.Cells[1].Value.ToString() + " ?"))
                {
                    if(frmcf.ShowDialog() == DialogResult.OK)
                        Delete(grDonVi.ActiveRow.Cells[0].Value.ToString(), grDonVi.ActiveRow.Cells[1].Value.ToString());
                }
                //if(MessageBox.Show("Xóa đơn vị " + grDonVi.ActiveRow.Cells[1].Value.ToString() + " ?","Xóa",MessageBoxButtons.YesNo,MessageBoxIcon.Warning) == DialogResult.Yes)
                //{
                    
                //}
                
            }
        }

        private void btnButton_Click(object sender, EventArgs e)
        {
            if (btnSave.Focused == true|| sender =="1")
                Save(_flag);
            else if (btnAdd.Focused == true || sender == "2")
                Clear();
            else if (btnExit.Focused == true || sender == "3")
                this.Close();
        }
        private void grDonVi_DoubleClick(object sender, EventArgs e)
        {
            txtDonVi.Text = grDonVi.ActiveRow.Cells[1].Value.ToString();
            txtMota.Text = grDonVi.ActiveRow.Cells[2].Value.ToString();
            _flag = true;
        }
        #endregion
        #region ----------------Thêm, Xóa, Sửa------------------------
        private void Save(bool flag)
        {
            if (flag == false)
            {
                object[] o = { "@Ten_DonVi", txtDonVi.Text.Trim(), "@MoTa", txtMota.Text.Trim() };
                int i = AccessDataLayer.ExecuteNonQuery("spDonVi_Add", CommandType.StoredProcedure, o);
                Clear();
            }
            else if (flag == true)
            {
                object[] o = { "@ID_DonVi",grDonVi.ActiveRow.Cells[0].Value,
                                 "@Ten_DonVi", txtDonVi.Text.Trim(), "@MoTa", 
                                 txtMota.Text.Trim() };
                int i = AccessDataLayer.ExecuteNonQuery("spDonVi_Update", CommandType.StoredProcedure, o);
                grDonVi.ActiveRow.Cells[1].Value = txtDonVi.Text.Trim();
                grDonVi.ActiveRow.Cells[2].Value = txtMota.Text.Trim();
                txtDonVi.Text = "";
                txtMota.Text = "";
                txtDonVi.Focus();
                txtDonVi.SelectAll();
                _flag = false;

                
                
            }
        }
        private void Delete( string ID,string TenDonVi)
        {
            object[] o = { "@ID_DonVi", ID };
            int i = AccessDataLayer.ExecuteNonQuery("spDonVi_Delete", CommandType.StoredProcedure, o);
            if (i > 0)
            {
                using (ErrorScreen ee = new ErrorScreen("Đã xóa đơn vị " + TenDonVi, "Thông Báo", 4))
                {
                    ee.ShowDialog();
                    grDonVi.DeleteSelectedRows(false);
                }
                
            }
            else
                using (ErrorScreen ee = new ErrorScreen("Đơn vị đã có liên kết dữ liệu, không thể xóa","Lỗi",4))
                {
                    ee.ShowDialog();
                }
        }
        #endregion
       

      
        
    }
}
